using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_PlaceCategoryManagement : admin_BaseUserControl
{
    PlaceCategoryDAO daoPlaceCate = new PlaceCategoryDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "place";
        LoadData();        
            
    }
    public void LoadData()
    {
        dgrPlaceCate.DataSource = daoPlaceCate.GetAllPlaceCategories();
        dgrPlaceCate.DataBind();
    }
    protected void lnkAddForm_Click(object sender, EventArgs e)
    {
        pnlAddBox.Visible = true;
    }
    protected void lnkHide_Click(object sender, EventArgs e)
    {
        pnlAddBox.Visible = false;
    }
    protected void dgrPlaceCate_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgrPlaceCate.EditIndex = e.NewEditIndex;
        LoadData();
    }
    protected void dgrPlaceCate_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgrPlaceCate.EditIndex = -1;
        LoadData();
    }
    protected void dgrPlaceCate_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(dgrPlaceCate.DataKeys[e.RowIndex].Value.ToString());
        if (daoPlaceCate.Delete(id) > 0)
        {
            ShowMessage("The category is deleted");
        }
        else ShowMessage("Error occured when trying to delete the category");
        LoadData();
    }
    protected void dgrPlaceCate_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlaceCategory placeCate = new PlaceCategory();
        int id = int.Parse(dgrPlaceCate.DataKeys[e.RowIndex].Value.ToString());
        placeCate.Id = id;
        placeCate.Name=((TextBox)(dgrPlaceCate.Rows[e.RowIndex]).FindControl("txtRowName")).Text;
        placeCate.State=((CheckBox)(dgrPlaceCate.Rows[e.RowIndex]).FindControl("chkRowState")).Checked;
        placeCate.ModifiedDate = DateTime.Now;
        placeCate.ModifiedBy = 1;
        if (daoPlaceCate.Update(placeCate) > 0)
        {
            ShowMessage("The category is updated");
        }
        else ShowMessage("Error occured when trying to updated the category");
        dgrPlaceCate.EditIndex = -1;
        LoadData();
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        PlaceCategory placeCate = new PlaceCategory();
        placeCate.Name = txtName.Text;
        placeCate.State = chkState.Checked;
        placeCate.ModifiedDate = DateTime.Now;
        placeCate.ModifiedBy = 1;
        if (daoPlaceCate.Insert(placeCate) > 0)
        {
            ShowMessage("New category is created");
            txtName.Text = "";
            chkState.Checked = false;
            LoadData();
        }
        else ShowMessage("Error occured when trying to create new category");
    }
   
}
